package com.leo.dao;

import com.leo.base.db.annotation.DynamicDataSource;
import com.leo.base.db.annotation.GlobalDataSource;
import com.leo.base.db.annotation.ShardingDataSource;
import com.leo.base.db.annotation.UnionDataSource;
import com.leo.bean.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author wangss
 */
@Mapper
public interface StudentMapper {
    @Insert("insert into student(sno, sname, ssex) values(#{sno}, #{name}, #{sex})")
    @ShardingDataSource(tableName = "shardingStudent")
    int add(Student student);

    @Update("update student set sname=#{name}, ssex=#{sex} where sno=#{sno}")
    @ShardingDataSource(tableName = "shardingStudent")
    int update(Student student);

    @Delete("delete from student where sno=#{sno}")
    @ShardingDataSource(tableName = "shardingStudent")
    int deleteBysno(String sno);

    @Select("select * from student where sno=#{sno}")
    @Results(id = "student", value = {
            @Result(property = "sno", column = "sno", javaType = String.class),
            @Result(property = "name", column = "sname", javaType = String.class),
            @Result(property = "sex", column = "ssex", javaType = String.class)
    })
    @UnionDataSource(tableName = "unionStudent")
    Student queryStudentBySno(String sno);

    @Select("select * from student")
    @Results({
            @Result(property = "sno", column = "sno", javaType = String.class),
            @Result(property = "name", column = "sname", javaType = String.class),
            @Result(property = "sex", column = "ssex", javaType = String.class)
    })
    @UnionDataSource(tableName = "unionStudent")
    List<Student> queryStudent();

    @Select("select * from student")
    @Results({
            @Result(property = "sno", column = "sno", javaType = String.class),
            @Result(property = "name", column = "sname", javaType = String.class),
            @Result(property = "sex", column = "ssex", javaType = String.class)
    })
    @GlobalDataSource(tableName = "globalStudent")
    List<Student> queryStudentGlobal();

    @Insert("insert into student(sno, sname, ssex) values(#{sno}, #{name}, #{sex})")
    @DynamicDataSource(value = "mysql")
    int addDynamic(Student student);
}
